package apps

import (
	"fmt"

	v1 "gitee.com/lcc_dr/restful-api-demo/api/book/v1"
	"gitee.com/lcc_dr/restful-api-demo/apps/host"
	"github.com/emicklei/go-restful/v3"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
)

var (
	HostService host.HostService
	// Ginservice  ServiceGin
	BookService v1.ServiceServer

	ServiceImpls        = map[string]ServiceImpl{}
	ServicGins          = map[string]ServiceGin{}
	ServicBookImpls     = map[string]ServiceBookImpl{}
	ServicBookGrpcImpls = map[string]ServiceBookGrpcImpl{}
	ServicRestfuls      = map[string]ServiceRestful{}
)

//注册host 服务到ioc
func RegistryImpl(svc ServiceImpl) {

	if _, ok := ServiceImpls[svc.Name()]; ok {
		panic(fmt.Sprintf("service %s has registried", svc.Name()))
	}
	ServiceImpls[svc.Name()] = svc
	if v, ok := svc.(host.HostService); ok {
		HostService = v //host.HostService 的具体实现
	}

}

//注册http-gin服务到ioc
func RegistryGin(svc ServiceGin) {

	if _, ok := ServicGins[svc.Name()]; ok {
		panic(fmt.Sprintf("gin service %s has registried", svc.Name()))
	}
	ServicGins[svc.Name()] = svc

}

//注册book服务到ioc
func RegistryBookImpl(svc ServiceBookImpl) {

	if _, ok := ServicBookImpls[svc.Name()]; ok {
		panic(fmt.Sprintf("book-service %s has registried", svc.Name()))
	}
	ServicBookImpls[svc.Name()] = svc
	if v, ok := svc.(v1.ServiceServer); ok {
		BookService = v //host.HostService 的具体实现
	}

}

//注册book-grpc服务到ioc
func RegistryGrpcImpl(svc ServiceBookGrpcImpl) {

	if _, ok := ServicBookGrpcImpls[svc.Name()]; ok {
		panic(fmt.Sprintf("grpc-service %s has registried", svc.Name()))
	}
	ServicBookGrpcImpls[svc.Name()] = svc

}

//注册restful服务到ioc
func RegistryRestful(svc ServiceRestful) {

	if _, ok := ServicRestfuls[svc.Name()]; ok {
		panic(fmt.Sprintf("grpc-service %s has registried", svc.Name()))
	}
	ServicRestfuls[svc.Name()] = svc

}

func GetImpl(name string) interface{} {

	for k, v := range ServiceImpls {

		if k == name {

			return v
		}
	}
	return nil

}
func GetBookImpl(name string) interface{} {

	for k, v := range ServicBookImpls {

		if k == name {

			return v
		}
	}
	return nil

}

type ServiceImpl interface {
	Config()
	Name() string
}

type ServiceGin interface {
	Registry(gin.IRouter)
	Config()
	Name() string
}

type ServiceBookImpl interface {
	Config()
	Name() string
}

type ServiceBookGrpcImpl interface {
	Config()
	Name() string
	Registry(*grpc.Server)
}

type ServiceRestful interface {
	Config()
	Name() string
	Registry(*restful.WebService)
}

func InitImpl() {

	for _, svc := range ServiceImpls {
		svc.Config() // 加载 host service

	}

	for _, svc := range ServicBookImpls {
		svc.Config() // 加载 book service

	}

}
func InitGin(r gin.IRouter) {

	for _, svc := range ServicGins {

		svc.Config() // 加载 svc logger 、db具体配置

	}
	for _, svc := range ServicGins {
		svc.Registry(r)
	}

}

func InitGrpc(g *grpc.Server) {

	for _, svc := range ServicBookGrpcImpls {

		svc.Config() // 加载 svc logger 、db具体配置

	}
	for _, svc := range ServicBookGrpcImpls {

		svc.Registry(g) // 加载 svc logger 、db具体配置

	}

}

// func InitRestful(restc *restful.Container) {
func InitRestful(restc *restful.Container) {
	for _, svc := range ServicRestfuls {

		svc.Config() // 加载 svc logger 、db具体配置

	}
	for _, svc := range ServicRestfuls {
		ws := new(restful.WebService)
		restc.Add(ws)

		svc.Registry(ws) // 加载 svc logger 、db具体配置

	}

}
